package comchifaner.util;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

public class IDUtils {
	public static OrderIdGenerator orderIdGenerator = new OrderIdGenerator();
	public static UUIdGenerator uuidGenerator = new UUIdGenerator();

	public static String nextOrderId() {
		return orderIdGenerator.nextId();
	}

	public static String orderIdMean(String id) {
		return orderIdGenerator.mean(id);
	}

	public static String nextUUId() {
		return uuidGenerator.nextId();
	}
	public static String getSupplierCode(){
		SimpleDateFormat smf = new SimpleDateFormat("yyHHmmssMMdd");
		String str = "gh"+smf.format(new Date());
		return str;
	}
	private static class OrderIdGenerator {
		private static int count = 0;
		private static int maxCountInSecond = 1000;
		private static String lastIpSection = "";
		private static int randomInt;
		private static final int totalChars = 10 + 26 + 26;
		private static char[] availiableChars;
		private static final int cropY = 14;

		public OrderIdGenerator() {
			try {
				String ip = InetAddress.getLocalHost().getHostAddress();
				lastIpSection = ip.substring(ip.lastIndexOf('.') + 1, ip.length());
				int ipInt = Integer.parseInt(lastIpSection);
				lastIpSection = Integer.toHexString(ipInt);

				randomInt = (int) (Math.random() * 10);

				availiableChars = new char[totalChars];
				int index = 0;
				int numberStart = 48;
				for (int i = 0; i < 10; i++) {
					char b = (char) (numberStart + i);
					availiableChars[index++] = b;
				}
				int uppercaseStart = 65;
				for (int i = 0; i < 26; i++) {
					if(i==8)continue;
					char b = (char) (uppercaseStart + i);
					availiableChars[index++] = b;
				}
				int lowercaseStart = 97;
				for (int i = 0; i < 26; i++) {
					if(i==11)continue;
					char b = (char) (lowercaseStart + i);
					availiableChars[index++] = b;
				}
				
			} catch (UnknownHostException e) {
				e.printStackTrace();
			}
		}

		public String nextId() {
			SimpleDateFormat smf = new SimpleDateFormat("yyMMddHHmmss");
			String str = smf.format(new Date());

			int y = doubleCharToInt(str.charAt(0), str.charAt(1));
			int M = doubleCharToInt(str.charAt(2), str.charAt(3));
			int d = doubleCharToInt(str.charAt(4), str.charAt(5));
			int H = doubleCharToInt(str.charAt(6), str.charAt(7));
			int m = doubleCharToInt(str.charAt(8), str.charAt(9));
			int s = doubleCharToInt(str.charAt(10), str.charAt(11));

			StringBuilder ret = projectCode(Projects.tuancan); // 0
			ret.append(rc(M)); // 1
			ret.append(rc(d)); // 2
			ret.append(randomInt); // 3
			ret.append(lastIpSection); // 45
			ret.append(String.format("%02d", (y - cropY))); // 67
			ret.append(rc(H)); // 8
			ret.append(rc(m)); // 9
			ret.append(rc(s)); // 10
			int countIndex = ++count % maxCountInSecond;
			ret.append(String.format("%03d", countIndex));
			return ret.toString();
		}

		public String mean(String id) {
			StringBuilder sb = new StringBuilder();
			int randomInt = id.charAt(3) - 48;

			char projectCode = id.charAt(0);
			sb.append(" 项目名称:");
			sb.append(getProjectNameByValue(getValueByChar(projectCode, randomInt)));

			sb.append(" ID生成服务IP:");
			sb.append(doubleCharToInt(id.charAt(4), id.charAt(5), 16));

			sb.append(" 时间:");
			sb.append(doubleCharToInt(id.charAt(6), id.charAt(7)) + cropY);
			sb.append("年,");
			sb.append(getValueByChar(id.charAt(1), randomInt));
			sb.append("月,");
			sb.append(getValueByChar(id.charAt(2), randomInt));
			sb.append("日   ");
			sb.append(getValueByChar(id.charAt(8), randomInt));
			sb.append(":");
			sb.append(getValueByChar(id.charAt(9), randomInt));
			sb.append(":");
			sb.append(getValueByChar(id.charAt(10), randomInt));

			sb.append("  第");
			sb.append(id.charAt(11));
			sb.append(id.charAt(12));
			sb.append(id.charAt(13));
			sb.append("个订单");

			return sb.toString();
		}

		// Random index for array then return the char of the index.
		private char rc(int value) {
			int v = randomInt + value;
			if (v >= totalChars) {
				v = v % totalChars;
			}
			return availiableChars[v];
		}

		private int doubleCharToInt(char c1, char c2, int radix) {
			int i1 = Character.digit(c1, 10);
			int i2 = Character.digit(c2, 10);
			return i1 * radix + i2;
		}

		private int doubleCharToInt(char c1, char c2) {
			int i1 = Character.digit(c1, 10);
			int i2 = Character.digit(c2, 10);
			return i1 * 10 + i2;
		}

		private StringBuilder projectCode(Projects project) {
			StringBuilder sb = new StringBuilder();
			sb.append(rc(project.val()));
			return sb;
		}

		private int getValueByChar(char c, int random) {
			for (int i = 0; i < totalChars; i++) {
				char tmp = availiableChars[i];
				if (c == tmp) {
					if (i < random) {
						i = i + totalChars - random;
					} else {
						i -= random;
					}
					return i;
				}
			}
			return -1;
		}

		private String getProjectNameByValue(int v) {
			String projectName = "不知道的项目名称";
			if (v == Projects.tuancan.val()) {
				projectName = Projects.tuancan.pName();
			}
			return projectName;
		}

	}

	private static class UUIdGenerator {
		public String nextId() {
			return UUID.randomUUID().toString();
		}
	}

	private static enum Projects {
		tuancan(1, "吃饭儿进销存系统");

		private int val;
		private String name;

		private Projects(int value, String name) {
			this.val = value;
			this.name = name;
		}

		public int val() {
			return this.val;
		}

		public String pName() {
			return this.name;
		}
	}
}
